package com.sfh.dog.ui.fragment;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.provider.CalendarContract.Calendars;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;

import com.sfh.dog.DogApplication;
import com.sfh.dog.R;
import com.sfh.dog.adapter.UserDailyAdapter;
import com.sfh.dog.entity.Daily;
import com.sfh.dog.util.LogUtil;

/**
 * 达宠
 * @author sWX229906
 *
 */
public class FastFragment extends AFragment
{
    
    private UserDailyAdapter adapter;
    
    private List<Daily> dailyEntities = new ArrayList<Daily>();
    
    @Override
    public void getData()
    {
        BmobQuery<Daily> query = new BmobQuery<Daily>();
        query.setSkip(10 * page);
        query.setLimit(10 * (page + 1));
        query.order("-glamour");
        query.findObjects(getActivity(), new FindListener<Daily>()
        {
            @Override
            public void onSuccess(List<Daily> arg0)
            {
                if (page == 0)
                {
                    dailyEntities.clear();
                }
                dailyEntities.addAll(arg0);
            //    Collections.sort(dailyEntities, comparator);
                adapter.notifyDataSetChanged();
                mPullRefreshListView.onRefreshComplete();
            }
            
            @Override
            public void onError(int arg0, String arg1)
            {
                mPullRefreshListView.onRefreshComplete();
                
            }
        });
        
    }
    
    public String getLastDate(int lastday)
    {
        long time = System.currentTimeMillis() - lastday * 24 * 60 * 60 * 1000;
        // 2014-10-16 09:48:34
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        
        String m = year + "-" + month + "-" + day + " 00:00:00";
        LogUtil.e("7 前=" + m);
        return m;
    }
    
    public String geDate()
    {
        // 2014-10-16 09:48:34
        long time = System.currentTimeMillis();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String m = format.format(new java.util.Date(time)) + " 23:59:59";
        LogUtil.e("当前前=" + m);
        return m;
    }
    
    Comparator<Daily> comparator = new Comparator<Daily>()
    {
        @Override
        public int compare(Daily lhs, Daily rhs)
        {
            int lhsCount = getCount(lhs.getPraiseSize(), lhs.getReviewSize());
            int rhsCount = getCount(rhs.getPraiseSize(), rhs.getReviewSize());
            return lhsCount > rhsCount ? 1 : 0;
        }
        
        private int getCount(int zan, int revice)
        {
            return zan * 6 + revice * 8 + 8;
        }
    };
    
    @Override
    public void getInit()
    {
        adapter = new UserDailyAdapter(getActivity(), dailyEntities);
        mPullRefreshListView.setAdapter(adapter);
    }
    
    @Override
    public void select(int position)
    {
        Daily entity = dailyEntities.get(position - 1);
        DogApplication.gotoDailyDetailUI(getActivity(), entity);
    }
    
    @Override
    public int Layout()
    {
        return R.layout.main_fragment;
    }
}
